<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<html>

<head>
    <meta charset="utf-8">
    <title>物料列表信息</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="js/user.js"></script>
    <link rel="stylesheet" href="css/user.css">
    <style>
        td, th {
            text-align: center;
        }

        .custom-smaller-input {
            font-size: 1.25rem;
        }
    </style>
</head>

<body>

<div class="container-fluid">
    <div class="row">

        <div class="col-sm-12" id="right">
            <ol class="breadcrumb">
                <li class="active">菜单
                </li>
                <li class="active">物料信息
                </li>
            </ol>
            <%
                String itemId = "";
                String itemName = "";
                String itemClasses = "";
                String vendorName = "";
                String site = "";
                String inboundTime = "";
                String contact = "";
                String phoneNumber = "";
                if (request.getParameter("itemId") != null) itemId = request.getParameter("itemId");
                if (request.getParameter("itemName") != null) itemName = request.getParameter("itemName");
                if (request.getParameter("itemClasses") != null) itemClasses = request.getParameter("itemClasses");
                if (request.getParameter("vendorName") != null) vendorName = request.getParameter("vendorName");
                if (request.getParameter("site") != null) site = request.getParameter("site");
                if (request.getParameter("inboundTime") != null) inboundTime = request.getParameter("inboundTime");
                if (request.getParameter("contact") != null) contact = request.getParameter("contact");
                if (request.getParameter("phoneNumber") != null) phoneNumber = request.getParameter("phoneNumber");
            %>
            <div class="panel panel-default">
                <div class="panel-heading">
                    搜索
                </div>
                <div class="panel-body">
                    <form role="form" class="form-inline" action="material_list.jsp" method="post">
                        <div class="form-group">
                            <label>物料编号</label>
                            <input type="text" class="form-control custom-smaller-input" id="itemId"
                                   name="itemId" value="<%=itemId%>" placeholder="请输入物料编号">
                        </div>
                        <div class="form-group">
                            <label>物料名称</label>
                            <input type="text" class="form-control custom-smaller-input" id="itemName" name="itemName"
                                   value="<%=itemName%>" placeholder="请输入物料名称">
                        </div>
                        <div class="form-group">
                            <label>材料类型</label>
                            <select class="form-control" id="itemClasses" name="itemClasses">
                                <option value="">请选择</option>
                                <option value="金属" <%=itemClasses.equals("金属") ? "selected" : "" %>>金属</option>
                                <option value="合金" <%=itemClasses.equals("合金") ? "selected" : "" %>>合金</option>
                                <option value="复合材料" <%=itemClasses.equals("复合材料") ? "selected" : "" %>>复合材料</option>
                                <option value="其它" <%=itemClasses.equals("其它") ? "selected" : "" %>>其它</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>供应商</label>
                            <input type="text" class="form-control custom-smaller-input" id="vendorName"
                                   name="vendorName"
                                   value="<%=vendorName%>" placeholder="请输入供应商名称">
                        </div>
                        <div class="form-group">
                            <label>电话</label>
                            <input type="text" class="form-control custom-smaller-input" id="phoneNumber"
                                   name="phoneNumber" value="<%=phoneNumber%>" placeholder="请输入联系人电话">
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">开始搜索</button>
                        </div>
                        <div class="form-group">
                            <a class="btn btn-default" href="material_add.jsp">添加</a>
                        </div>
                    </form>
                </div>
            </div>
            <!--
            列表展示
        -->
            <div class="table-responsive">
                <table class="table table-striped ">
                    <thead>
                    <tr>
                        <%--<th>序号</th>--%>
                        <th>物料编号</th>
                        <th>物料名称</th>
                        <th>材料类型</th>
                        <th>供应商名称</th>
                        <th>厂址</th>
                        <th>入库时间</th>
                        <th>联系人</th>
                        <th>手机号</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <%
                        Connection conn = (Connection) session.getAttribute("Connection");
                        String sql = "SELECT * FROM material WHERE 1=1";
                        if (!itemId.isEmpty()) {
                            sql += " AND itemId LIKE '%" + itemId + "%'";
                        }
                        if (!itemName.isEmpty()) {
                            sql += " AND itemName = '" + itemName + "'";
                        }
                        if (!itemClasses.isEmpty()) {
                            sql += " AND itemClasses = '" + itemClasses + "'";
                        }
                        if (!vendorName.isEmpty()) {
                            sql += " AND vendorName = '" + vendorName + "'";
                        }
                        if (!phoneNumber.isEmpty()) {
                            sql += " AND phoneNumber = '" + phoneNumber + "'";
                        }
                        int curpage = 1;
                        if (request.getParameter("page") != null) {
                            String pg = request.getParameter("page");
                            curpage = Integer.parseInt(pg);
                        }
                        int al = 0; // al为-1时表示上一页，为1时表示下一页，默认为0
                        if (request.getParameter("alterpage") != null) {
                            String alpg = request.getParameter("alterpage");
                            al = Integer.parseInt(alpg);
                        }
                        if (curpage > 1 || al > 0) {
                            curpage += al;
                        }
                        int page_size = 6;
                        int start = curpage * page_size - page_size;
                        String pge = " LIMIT " + start + ", " + page_size;  // 注意前面添加了一个空格,没这个空格就会报错
                        sql = sql + pge;

                        PreparedStatement pstmt = conn.prepareStatement(sql);
                        ResultSet rs = pstmt.executeQuery();



                        while (rs.next()) {
                    %>
                    <tr>
                        <td><%=rs.getString("itemId")%>
                        </td>
                        <td><%=rs.getString("itemName")%>
                        </td>
                        <td><%=rs.getString("itemClasses")%>
                        </td>
                        <td><%=rs.getString("vendorName")%>
                        </td>
                        <td><%=rs.getString("site")%>
                        </td>
                        <td><%=rs.getString("inboundTime")%>
                        </td>
                        <td><%=rs.getString("contact")%>
                        </td>
                        <td><%=rs.getString("phoneNumber")%>
                        </td>
                        <td>
                            <div class="btn-group">
                                <a class="btn btn-default" href="material_edit.jsp?id=<%=rs.getInt("id")%>">修改</a>
                                <a class="btn btn-danger"
                                   href="#" onclick="confirmDelete(<%=rs.getInt("id")%>)">删除</a>
                            </div>
                        </td>
                    </tr>
                    <%
                        }
                    %>
                    </tbody>
                </table>
            </div>
            <ul class="pagination" style="float: right;">
                <li>
                    <a href="material_list.jsp?alterpage=-1&page=<%=curpage%>&itemId=<%=itemId%>&itemName=<%=itemName%>&itemClasses=<%=itemClasses%>&vendorName=<%=vendorName%>&phoneNumber=<%=phoneNumber%>">&laquo;</a>
                </li>
                <%
                    int psize = 5; //页面号码标签数量
                    int ppg = psize * ((curpage - 1) / psize + 1);
                    for (int i = ppg - psize + 1; i <= ppg; i++) {
                        if (i == curpage) {
                            out.println("<li class=\"active\"><a href=\"material_list.jsp?page=" + i + "&itemId=" + itemId + "&itemName=" + itemName + "&itemClasses=" + itemClasses + "&vendorName=" + vendorName + "&phoneNumber=" + phoneNumber + "\">" + i + "</a></li>");

                        } else {
                            out.println("<li><a href=\"material_list.jsp?page=" + i + "&itemId=" + itemId + "&itemName=" + itemName + "&itemClasses=" + itemClasses + "&vendorName=" + vendorName + "&phoneNumber=" + phoneNumber + "\">" + i + "</a></li>");
                        }
                    }
                %>
                <li>
                    <a href="material_list.jsp?alterpage=1&&page=<%=curpage%>&itemId=<%=itemId%>&itemName=<%=itemName%>&itemClasses=<%=itemClasses%>&vendorName=<%=vendorName%>&phoneNumber=<%=phoneNumber%>">&raquo;</a>
                </li>
            </ul>
        </div>
    </div>
</div>

</body>
<script>
    function confirmDelete(id) {
        if (confirm("确定要删除吗？")) {
            window.location.href = "material_delete.jsp?id="+id+"&page=<%=curpage%>&itemId=<%=itemId%>&itemName=<%=itemName%>&itemClasses=<%=itemClasses%>&vendorName=<%=vendorName%>&phoneNumber=<%=phoneNumber%>";
        }
    }
</script>
</html>